var uploader;
var _queue=[];
function initWebuploader(id){
			var extensions='gif,jpg,jpeg,bmp,png,pdf,doc,docx,xls,xlsx,ppt,pptx,zip,rar';
			uploader = WebUploader.create({
				auto: true,
			    // swf文件路径
			    swf: '${ctx}/static/webuploader/Uploader.swf',
			    // 文件接收服务端。
			    server: kl.ctx +'/basic/attachment/busUpload/'+ id,
			    // 选择文件的按钮。可选。
			    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
			    pick: {
                id: '#picker',
                //只能选择一个文件上传
                //multiple: false
                },
                fileSingleSizeLimit: 100 * 1024 * 1024,   //设定单个文件大小
                //限制只能上传一个文件
                //fileNumLimit: 1,
			    // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
			    resize: false,
                accept: {
                    extensions: extensions,
                } ,
                //可重复上传
                duplicate: true/* ,
			    chunked: true,//分片上传-大文件的时候分片上传，默认false
                chunkSize: 500 * 1024 */
			});

			// 上传队列，仅包括等待上传的文件
		    
			uploader.on('fileQueued', function (file) {
	                 var $li = $('#'+file.id );
	                  $percent = $li.find('.info');
	                 if (!$percent.length) {
	                     $("#thelist").append('<div id="'+file.id+'" class="item">' +
	                      '<h4 class="info">' + file.name + '</h4>' +
	                      '<p class="state">等待上传...</p>' +
	                     ' <span id="iid"></span><span id="all"></span></div>');
	                     _queue.push(file);
	                 }
	                 else {
	                     var $lii = $('#'+file.id );//修改已上传的
	                     $lii.fadeIn();
	                     $lii.html('<div id="'+file.id+'" class="item">' +
	                    '<h4 class="info">' + file.name + '</h4>' +
	                    '<p class="state">等待上传...</p>' +
	                   '<span id="iid"></span><span id="all"></span></div>');

	                 }
	         });

			    
	            // 文件上传过程中创建进度条实时显示。
	         uploader.on('uploadProgress', function (file, percentage) {
	              $li = $('#'+file.id );
	              $percent = $li.find('.progress .progress-bar');

	             // 避免重复创建---进度条
	             if (!$percent.length) {
	                 $percent = $('<div class="progress ctive">' +
	                  '<div class="progress-bar" role="progressbar" style="width: 0%">' +
	                  '</div>' +
	                 '</div>').appendTo($li).find('.progress-bar');
	             }

	             $li.find('p.state').text('上传中');
	             $percent.css('width', percentage * 100 + '%');//进度条
	             //上传期间所上传占总的比例
	             var nowSize = 0;//已上传大小
	             var gb = Math.pow(1024, 3);
	             var mb = Math.pow(1024, 2);
	             var kb = 1024;
	             var fileSize = uploader.getFiles()[uploader.getFiles().length - 1].size;//总大小
	             if (fileSize >= gb) {
	                 var fz = (fileSize / gb).toFixed(2);
	                 Size = fz + "GB";
	                 nowSize = (fz * percentage).toFixed(2) + "GB";
	             }
	             else if (fileSize >= mb) {
	                 var fz = (fileSize / mb).toFixed(2);
	                 Size = fz + "MB";
	                 nowSize = (fz * percentage).toFixed(2) + "MB";
	             }
	             else if (fileSize >= kb) {
	                 var fz = (fileSize / kb).toFixed(2);
	                 Size = fz + "KB";
	                 nowSize = (fz * percentage).toFixed(2) + "KB";
	             }
	             else {
	                 var fz = fileSize;
	                 Size = fz + "B";
	                 nowSize = fz * percentage + "B";
	             }
	             //var iidSize = nowSize / Size;
	             $("#iid" ).html((percentage*100).toFixed(2)+"%");//已上传比例
	             $("#all" ).html("总大小：" + Size);//总大小

	         });
	         uploader.on('uploadSuccess', function (file, response) {
	             //console.log(response._raw);
	             $('#'+file.id).find('p.state').text('已完成');
	             uploader.removeFile(file);//删除
	             uploader.reset();
	             if(response.success){
		        		grid.datagrid("reload");
		        		$grid.datagrid("reload");
		        	}
		        	showInfo(response.msg);
	             
	         });

	         uploader.on('uploadError', function (file) {
	             $('#'+file.id).find('p.state').text('上传出错');
	         });
	         
	         uploader.on("uploadFinished", function () {
	        	 _queue=[];
	             uploader.destroy();
	             initWebuploader(id);
	         });

	         uploader.on('uploadComplete', function (file) {
	        	 var $li = $('#'+file.id );
	        	 $li.fadeOut();//删除进度条
	         });

	            /**
				* 验证文件格式以及文件大小
				*/
	         uploader.on("error", function (type) {

	             if (type == "F_EXCEED_SIZE") {
	                 alert("文件大小不能超过100M");
	             }if(type == "Q_TYPE_DENIED") {
	            	 alert("只能上传以下格式文件："+extensions);
	             }
	             else {
	                 alert("上传出错！请检查后重新上传！错误代码" + type);
	             }
	         });
		}